﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace OrderManager
{
    public partial class MaintainOrders : Form
    {
        List<string> oldOrders;
        Dictionary<string, string> orders;




        public MaintainOrders()
        {
            InitializeComponent();


            StreamReader input;
            oldOrders = new List<string>();
            orders = new Dictionary<string,string>();


            try
            {
                int j = 0;
                input = File.OpenText("Orders.txt");
                while (!input.EndOfStream)
                {
                    bool old = false;
                    string filename = input.ReadLine();
                    StreamReader source;

                    try
                    {
                        source = File.OpenText(@"orders\" + filename + ".txt");
                        orders[filename] = source.ReadToEnd();
                        source.Close();


                        source = File.OpenText(@"orders\" + filename + ".txt");


                        // A megrendelés sorait átugorja az 5. sorig, ami a dátum
                        for (int i = 0; i < 4; ++i)
                            source.ReadLine();
                        

                        // Dátum                        
                        string tmp = source.ReadLine();
                        tmp = tmp.Split(':')[1].TrimStart();
                        DateTime tmpDate = DateTime.Parse(tmp);

                        // Idő
                        tmp = source.ReadLine();
                        int hour = int.Parse(tmp.Split(':')[1].Substring(1));
                        int minute = int.Parse(tmp.Split(':')[2]);

                        // Idő hozzáadása a dátumhoz
                        try
                        {
                            tmpDate = tmpDate.AddHours(hour);
                            tmpDate = tmpDate.AddMinutes(minute);
                        }
                        catch (Exception exc)
                        {
                            MessageBox.Show("A bemeneti fájl sérült vagy nem olvasható");
                            this.Close();

                            return;
                        }

                        source.Close();

                        if (tmpDate <= DateTime.Now.AddDays(-30.0))
                            old = true;
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show("Hiba történt. A hiba leírása:\n" + exc.Message);
                        return;
                    }

                    if (old)
                    {
                        oldOrders.Add(filename);
                        this.orderList.Items.Add(filename);
                        this.orderList.SetItemCheckState(j, CheckState.Checked);
                        ++j;
                    }
                }
                input.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Hiba történt. A hiba leírása:\n" + exc.Message);
                this.Close();
                return;
            }
        }

        private void selectAll(object sender, EventArgs e)
        {
            for (int i = 0; i < orderList.Items.Count; ++i)
                orderList.SetItemCheckState(i, CheckState.Checked);
        }

        private void deselectAll(object sender, EventArgs e)
        {
            foreach (int i in orderList.CheckedIndices)
                orderList.SetItemCheckState(i, CheckState.Unchecked);
        }

        private void Confirm(object sender, EventArgs e)
        {
            StringBuilder strb = new StringBuilder();
            for (int i = 0; i < this.orderList.Items.Count; ++i)
            {
                if (this.orderList.GetItemChecked(i))
                    strb.AppendLine(orderList.Items[i].ToString());
            }


            ConfirmationWindow cw = new ConfirmationWindow("Törlés megerősítése", "Biztosan törlöd a következő megrendeléseket?\n\n" + strb.ToString());
            DialogResult dr = cw.ShowDialog();


            if (dr != DialogResult.Yes) return;
            

            for (int i = 0; i < this.orderList.CheckedItems.Count; ++i)
            {
                try
                {
                    File.Delete(@"orders\" + orderList.CheckedItems[i].ToString() + ".txt");
                    this.oldOrders.Remove(orderList.CheckedItems[i].ToString());
                    this.orders.Remove(orderList.CheckedItems[i].ToString());
                    // TODO: kijelölő lista frissítése
                }
                catch (Exception exc)
                {
                    MessageBox.Show("Hiba történt. A hiba leírása:\n" + exc.Message);
                }
            }

            
            try
            {
                StreamWriter output = File.CreateText("Orders.txt");

                for (int i = 0; i < this.orders.Count; ++i)
                    output.WriteLine(orders.Keys.ElementAt(i));
                
                output.Flush();
                output.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Hiba történt. A hiba leírása:\n" + exc.Message);
            }


            MessageBox.Show("A kijelölt megrendelések törölve.");
            this.Close();
            return;
        }

        private void Close(object sender, EventArgs e)
        {
            this.Close();
            return;
        }

        private void ShowDetails(object sender, EventArgs e)
        {
            try
            {
                StreamReader input;
                input = File.OpenText(@"orders\" + this.orderList.SelectedItem.ToString() + ".txt");

                this.orderDetails.Text = input.ReadToEnd();

                input.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Hiba történt a fájl előnézetének létrehozásakor.\nA hiba leírása:\n" + exc.Message);
            }

            return;
        }
    }
}
